package com.nmdl.gps;

import androidx.appcompat.app.AppCompatActivity;

import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;

public class MainActivity extends AppCompatActivity {

    private TextView text_gps_3;
    private Button btn_gps_3;
    private GPSLocationManager gpsLocationManager;
    private LocationClient mLocationClient;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text_gps_3 = (TextView) findViewById(R.id.text_gps_3);
        btn_gps_3 = (Button) findViewById(R.id.btn_gps_3);
        initData();
//        initBaidu();
    }

    private void initBaidu() {
        SDKInitializer.initialize(getApplicationContext());
        btn_gps_3.setOnClickListener(view -> {
            if (mLocationClient == null) {
                mLocationClient = new LocationClient(MainActivity.this);
                LocationClientOption option = new LocationClientOption();
                option.setOpenGps(true); // 打开gps
                option.setCoorType("bd09ll"); // 设置坐标类型
                option.setScanSpan(1000);
                option.setIsNeedAddress(true);
                mLocationClient.setLocOption(option);
                MyLocationListener myLocationListener = new MyLocationListener();
                mLocationClient.registerLocationListener(myLocationListener);
            }
            mLocationClient.start();
        });
    }

    private void initData() {
        gpsLocationManager = GPSLocationManager.getInstances(MainActivity.this);
        btn_gps_3.setOnClickListener(v -> {
            //开启定位
            gpsLocationManager.start(new MyListener());
        });
    }

    class MyListener implements GPSLocationListener {

        @Override
        public void UpdateLocation(Location location) {
            if (location != null) {
                text_gps_3.setText("经度：" + location.getLongitude() + "\n纬度：" + location.getLatitude());
            }
        }

        @Override
        public void UpdateStatus(String provider, int status, Bundle extras) {
            if ("gps" == provider) {
                Toast.makeText(MainActivity.this, "定位类型：" + provider, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void UpdateGPSProviderStatus(int gpsStatus) {
            switch (gpsStatus) {
                case GPSProviderStatus.GPS_ENABLED:
                    Toast.makeText(MainActivity.this, "GPS开启", Toast.LENGTH_SHORT).show();
                    break;
                case GPSProviderStatus.GPS_DISABLED:
                    Toast.makeText(MainActivity.this, "GPS关闭", Toast.LENGTH_SHORT).show();
                    break;
                case GPSProviderStatus.GPS_OUT_OF_SERVICE:
                    Toast.makeText(MainActivity.this, "GPS不可用", Toast.LENGTH_SHORT).show();
                    break;
                case GPSProviderStatus.GPS_TEMPORARILY_UNAVAILABLE:
                    Toast.makeText(MainActivity.this, "GPS暂时不可用", Toast.LENGTH_SHORT).show();
                    break;
                case GPSProviderStatus.GPS_AVAILABLE:
                    Toast.makeText(MainActivity.this, "GPS可用啦", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(final BDLocation location) {
            runOnUiThread(() -> {
                if (location.getLatitude() != 4.9E-324) {
//                mLocationClient.stop();
                    System.out.println("经纬度正确：经度" + location.getLatitude() + "  纬度："+location.getLongitude());
                } else {
                    System.out.println("经纬度错误：经度" + location.getLatitude());
                }
                text_gps_3.setText("经纬度信息：经度" + location.getLatitude() + "  纬度："+location.getLongitude());
            });
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在onPause()方法终止定位
        if (gpsLocationManager != null) gpsLocationManager.stop();
    }
}